/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       text_edit_window.h

	$Header: /game/text_edit_window.h $

	$NoKeywords: $

 ************************************************************************/
#ifndef TEXT_EDIT_WINDOW_H_INCLUDED
#define TEXT_EDIT_WINDOW_H_INCLUDED

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "handler.h"
#include "text_window.h"

class t_text_edit_window;

typedef t_handler_1< t_text_edit_window* > t_text_edit_handler;
// --------------------------------------------------------------
// text window class
// --------------------------------------------------------------
class t_text_edit_window : public t_text_window
{
public:
	t_text_edit_window( t_cached_font& font, t_screen_rect const& rect, t_window* parent = 0,
		                std::string const& text = "", 
						t_pixel_24 const& color = t_pixel_24(0,0,255));

	void				 enable( bool arg );	
	virtual void		 begin_edit();
	virtual void		left_button_down( t_mouse_event const& event );
    virtual void		left_button_up( t_mouse_event const& event );
	virtual void		 on_keyboard_focus_lost();
	virtual void		 on_size_change( t_screen_rect const& rect );
	// called when text changes for any reason.
	void                 set_change_handler( t_text_edit_handler handler );
	// called when all edits are complete.
	void                 set_end_edit_handler( t_text_edit_handler handler );
protected:
	void         build_caret_window();
	void         delete_char();
	virtual void on_text_change();
	void         position_caret();

	int					m_caret_column;
	int					m_caret_row;
	int					m_caret_string_position;
	t_window*			m_caret_window;
	t_text_edit_handler m_change_handler;
	t_pixel_24			m_color;
	bool				m_editing;
	bool				m_enabled;
	t_text_edit_handler m_end_handler;
	int					m_rows;
};

inline void t_text_edit_window::enable( bool arg )
{
	m_enabled = arg;
	if (m_editing && !arg)
		on_keyboard_focus_lost();
}

inline void t_text_edit_window::set_change_handler( t_text_edit_handler handler )
{
	m_change_handler = handler;
}

inline void t_text_edit_window::set_end_edit_handler( t_text_edit_handler handler )
{
	m_end_handler = handler;
}

#endif // TEXT_EDIT_WINDOW_H_INCLUDED